package com.ogms.dge.container.modules.data.converter;

import com.ogms.dge.container.modules.app.service.impl.UserServiceImpl;
import com.ogms.dge.container.modules.data.entity.RecordEntity;
import com.ogms.dge.container.modules.data.service.RecordService;
import com.ogms.dge.container.modules.data.service.ServiceService;
import com.ogms.dge.container.modules.data.vo.RecordVo;
import com.ogms.dge.container.modules.sys.service.SysUserService;
import org.mapstruct.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @name: RecordConverter
 * @description: TODO
 * @author: Lingkai Shi
 * @date: 9/23/2024 4:13 PM
 * @version: 1.0
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface RecordConverter {

    @Mapping(target = "username", ignore = true)
    @Mapping(target = "dsName", ignore = true)
    RecordVo po2Vo(RecordEntity recordEntity, @Context SysUserService userService, @Context ServiceService serviceService);

    default List<RecordVo> poList2VoList(List<RecordEntity> recordEntityList, @Context SysUserService userService, @Context ServiceService serviceService) {
        return recordEntityList.stream()
                .map(record -> po2Vo(record, userService, serviceService))
                .collect(Collectors.toList());
    }

    @AfterMapping
    default void handleCustomFields(RecordEntity recordEntity, @MappingTarget RecordVo recordVo, @Context SysUserService userService, @Context ServiceService serviceService) {
        recordVo.setUsername(userService.getById(recordEntity.getUserId()).getUsername());
        recordVo.setDsName(serviceService.getById(recordEntity.getDsId()).getName());
    }
}
